package kr.saeildamil.androidstudy_second.multimedia;

import java.io.IOException;

import kr.saeildamil.androidstudy_second.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;

public class MPTest extends Activity {
	MediaPlayer mPlayer;
	String mSdPath;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch33_mptest);
		
		mPlayer = MediaPlayer.create(this, R.raw.dingdong);
		mSdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
	}	
	
	@Override
	protected void onPause() {
		super.onPause();
		if(mPlayer != null){
			mPlayer.release();
			mPlayer = null;
		}
	}
	
	public void mOnClick(View v){
		MediaPlayer player;
		switch(v.getId()){
		case R.id.btn1:
			player = MediaPlayer.create(this, R.raw.dingdong);
			player.start();
			break;
		case R.id.btn2:
			player = new MediaPlayer();
			try {
				player.setDataSource(mSdPath + "/oldpop.mp3");
				player.prepare();
				player.start();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case R.id.btn3:
			player = new MediaPlayer();
			Uri uri = Uri.parse("http://www.soen.kr/data/saemaul1.mp3");
			try {
				player.setDataSource(this,uri);
				player.prepare();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			player.start();
			break;
		case R.id.btn4:
			mPlayer.seekTo(0);
			mPlayer.start();
			break;
		case R.id.btn5:
			player = new MediaPlayer();
			try {
				player.setDataSource(mSdPath + "oldpop.mp3");
				player.start();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		case R.id.btn6:
			player = MediaPlayer.create(this, R.raw.dingdong);
			try {
				player.setDataSource(mSdPath + "oldpop.mp3");
				player.prepare();
				player.start();
				
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		}
	}
}
